package com.easygem.controller.front;

import com.easygem.common.error.SystemException;
import com.easygem.common.model.system.Dept;
import com.easygem.common.model.system.User;
import com.easygem.common.response.ActiveUser;
import com.easygem.common.response.ResponseBean;
import com.easygem.common.vo.front.SeatVO;
import com.easygem.front.service.ZoningService;
import io.swagger.annotations.ApiOperation;
import lombok.Synchronized;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author qq240
 * @version 1.0
 * @className ZoningController
 * @Description TODO 分区制坐席控制
 * @date 2025/4/17 16:46
 */
@RestController
@RequestMapping("/system/zoning")
public class ZoningController {
    @Autowired
    private ZoningService zoningService;

    @GetMapping("/getZoningStatus")
    public ResponseBean getZoningStatus(@RequestParam("accountName") String accountName) {
        Integer isZoning = zoningService.isZoning(accountName);
        if (isZoning==null){
            return ResponseBean.error("查询失败，isZoning为空");
        }
        return ResponseBean.success(isZoning);
    }

    @GetMapping("/openZoning")
    @Transactional
    @Synchronized
    public ResponseBean openZoning() {
        ActiveUser activeUser = (ActiveUser) SecurityUtils.getSubject().getPrincipal();
        User user = activeUser.getUser();
        if (user == null) {
            return ResponseBean.error("登录状态异常，终止操作");
        }
        if (user.getType()!=0){
            return ResponseBean.error("非超级管理员，无法操作，请停止你的危险行为!");
        }
        Integer flag= zoningService.openZoning(user);

        if (flag>0){
            //给下级部门修改次级部门Id
            Integer num = zoningService.updateSubDeptIdByDeptId(user.getDeptId());
            zoningService.updateAdminDeptIdByDeptId(user.getDeptId());
            if (num==0){
                return ResponseBean.error("开启失败，暂无下级部门,请添加二级部门后重试");
            }
            //重置该根部门下的坐席
            zoningService.resetSeatsStatusByDeptId(user.getDeptId());
            //给下级部门修改可用坐席数
            Map<String, Object> map = new HashMap<>();
            map.put("isZoning",zoningService.isZoning(user.getAccountName()));
            map.put("success","开启成功");
            return ResponseBean.success(map);
        }else {
            return ResponseBean.error("开启失败，请联系管理员检查操作日志");
        }
    }
    @GetMapping("/closeZoning")
    public ResponseBean closeZoning() {
        ActiveUser activeUser = (ActiveUser) SecurityUtils.getSubject().getPrincipal();
        User user = activeUser.getUser();
        if (user == null) {
            return ResponseBean.error("登录状态异常，终止操作");
        }
        if (user.getType()!=0){
            return ResponseBean.error("非超级管理员，无法操作，请停止你的危险行为!");
        }
        Integer flag= zoningService.closeZoning(user);
        if (flag>0){
            //给下级部门清空次级部门Id
             zoningService.cleanSubDeptIdByDeptId(user.getDeptId());
            Map<String, Object> map = new HashMap<>();
            map.put("isZoning",zoningService.isZoning(user.getAccountName()));
            map.put("success","关闭成功");
            return ResponseBean.success(map);
        }else {
            return ResponseBean.error("关闭失败，请联系管理员检查操作日志");
        }
    }

    /**
     * 查询二级部门信息
     * @return
     */
    @GetMapping("/getVFPDeptInfo")
    public ResponseBean getVFPDeptInfo(){
        ActiveUser activeUser = (ActiveUser) SecurityUtils.getSubject().getPrincipal();
        User user = activeUser.getUser();
        if (user == null) {
            return ResponseBean.error("登录状态异常，终止操作");
        }

        List<Dept> deptList = zoningService.getVFPDeptInfo(user.getDeptId());
        if (deptList!=null){
            return ResponseBean.success(deptList);
        }
        return ResponseBean.error("查询失败，没有二级部门");
    }

//    /**
//     * 查询可用坐席数
//     * @return
//     */
//    @GetMapping("/getAvailableSeatsNum")
//    public ResponseBean getAvailableSeatsNum(){
//        ActiveUser activeUser = (ActiveUser) SecurityUtils.getSubject().getPrincipal();
//        User user = activeUser.getUser();
//        if (user == null) {
//            return ResponseBean.error("登录状态异常，终止操作");
//        }
//        List<Dept> deptList = zoningService.selectAvailableSeatsNum(user.getDeptId());
//        if (deptList!=null){
//            return ResponseBean.success(deptList);
//        }
//        return ResponseBean.error("查询失败，没有二级部门");
//    }

    @GetMapping("/getAdminSeatsNum")
    public ResponseBean getAdminSeatsNum() {
        ActiveUser activeUser = (ActiveUser) SecurityUtils.getSubject().getPrincipal();
        User user = activeUser.getUser();
        Integer adminSeatsNum = zoningService.getAdminSeatsNum(user.getDeptId());
        if (adminSeatsNum==null){
            return ResponseBean.error("该账户没有配置坐席数，请联系管理员配置");
        }
        return ResponseBean.success(adminSeatsNum);
    }

    @PostMapping("/updateAllotSeatsByList")
    public ResponseBean updateAllotSeatsByList(@RequestBody List<Dept> dept) {
        if (dept==null){
            return ResponseBean.error("更新失败，值传输异常");
        }
        String msg = zoningService.updateAllotSeatsByList(dept);
        if (msg.equals("")){
            return ResponseBean.success("分配成功");
        }
        return ResponseBean.error(msg);

    }

    /**
     * 更新坐席状态(分区制版)
     * @param seatVO
     * @return
     * @throws SystemException
     */
    @PutMapping("/setZoningSeatStatus")
    public ResponseBean setZoningSeatStatus(@RequestBody SeatVO seatVO) throws SystemException {
        Integer flag = zoningService.setZoningSeatStatus(seatVO);
        if (flag > 0) {
            return ResponseBean.success("success");
        } else {
            return ResponseBean.error("error");
        }
    }
    @ApiOperation(value = "得到坐席最大数量与当前已使用的坐席量", notes = "得到坐席最大数量与当前已使用的坐席量")
    @GetMapping("/getZoningSeatsNumber")
    public ResponseBean<Map<String,Object>> getZoningSeatsNumber(@RequestParam("accountName") String accountName) {
        Map<String,Object> list=zoningService.selectZoningSeatsCount(accountName);
        return ResponseBean.success(list);
    }
}
